package com.evil.common.application.enums.judge;

import com.evil.common.core.enums.BaseEnum;
import com.evil.common.family.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * 匹配类型
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum MatchingTypeEnum {

    /**
     * 匹配类型（1-自定义，2-匹配字段）
     */
    CUSTOM(1, "自定义"),
    MATCHING_FIELD(2, "匹配字段"),
    DYNAMIC(3, "动态"),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;

    public static boolean isMatchingField(Integer id) {
        return null != id && MATCHING_FIELD.id == id;
    }

    public static boolean isDynamic(Integer id) {
        return null != id && DYNAMIC.id == id;
    }

    public static Optional<MatchingTypeEnum> findById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static MatchingTypeEnum isExistById(Integer id) {
        return MatchingTypeEnum.findById(id).orElseThrow(JudgeTypeEnum::getBusinessException);
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(BaseEnum.ERROR_PARAMETER, "匹配类型参数错误");
    }
}
